package it.mp.multitask.bean;

import org.xmlpull.v1.XmlPullParser;

import it.mp.multitask.R;
import it.mp.multitask.Setup;
import it.mp.multitask.utility.IconManager;
import android.app.Activity;
import android.app.ActivityManager.RecentTaskInfo;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.pm.ComponentInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.SpannedString;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class App {
	private String packageName;
	private String className;
	private boolean isGarbaged = false;
	private boolean showRecycle = false;
	private int tipo = 0;
	private int resIdBackgroundText = R.drawable.background_shape;
	private IconManager iconManager;
	private ComponentInfo ci = null;
	private PackageManager pm = null;

	private OnClickListener oclStartActivity = null;
	private OnLongClickListener oclKillActivity = null;
	private Activity parent = null;
	private StringBuffer sb;
	private Resources res;

	public void setOnClickListener(OnClickListener oclStartActivity) {
		this.oclStartActivity = oclStartActivity;
	}
	public void setOnLongClickListener(OnLongClickListener oclKillActivity) {
		this.oclKillActivity = oclKillActivity;
	}
	public App(Activity parent, RecentTaskInfo reti, IconManager iconManager) throws NameNotFoundException {
		this.parent = parent;
		res = this.parent.getResources();
		this.pm = parent.getPackageManager();
		packageName = reti.baseIntent.getComponent().getPackageName();
		className = reti.baseIntent.getComponent().getClassName();

		ci = parent.getPackageManager().getActivityInfo(reti.baseIntent.getComponent(), 0);

		this.iconManager = iconManager;
		tipo = 4;
		sb = new StringBuffer(packageName);
		sb.append(Setup.DELIMITER);
		sb.append(className);

	}
	public App(Activity parent, RunningTaskInfo rti, boolean showRecycle, IconManager iconManager) throws NameNotFoundException {
		this.parent = parent;
		res = this.parent.getResources();
		this.pm = parent.getPackageManager();
		packageName = rti.baseActivity.getPackageName();
		className = rti.baseActivity.getClassName();
		isGarbaged = rti.numRunning == 0;
		this.showRecycle = showRecycle;
		this.iconManager = iconManager;
		tipo = 1;

		ci = parent.getPackageManager().getActivityInfo(rti.baseActivity, 0);

		sb = new StringBuffer(packageName);
		sb.append(Setup.DELIMITER);
		sb.append(className);

	}
	public App(Activity parent, String packageName, String className, IconManager iconManager) throws NameNotFoundException {
		this.parent = parent;
		res = this.parent.getResources();
		this.pm = parent.getPackageManager();
		this.packageName = packageName;
		this.className = className;
		this.iconManager = iconManager;

		ci = parent.getPackageManager().getActivityInfo(new ComponentName(packageName, className), 0);

		sb = new StringBuffer(packageName);
		sb.append(Setup.DELIMITER);
		sb.append(className);

	}
	public App(Activity parent, String component, IconManager iconManager) throws NameNotFoundException {
		this.parent = parent;
		res = this.parent.getResources();
		this.pm = parent.getPackageManager();
		this.packageName = component.substring(0, component.indexOf(Setup.DELIMITER));
		this.className = component.substring(component.indexOf(Setup.DELIMITER) + Setup.DELIMITER.length());
		this.iconManager = iconManager;
		tipo = 2;

		ci = parent.getPackageManager().getActivityInfo(new ComponentName(packageName, className), 0);

		sb = new StringBuffer(packageName);
		sb.append(Setup.DELIMITER);
		sb.append(className);

	}
	public App(Activity parent, RunningServiceInfo rsi, boolean showRecycle, IconManager iconManager) {
		this.parent = parent;
		res = this.parent.getResources();
		this.pm = parent.getPackageManager();
		packageName = rsi.service.getPackageName();
		className = rsi.service.getClassName();
		isGarbaged = !rsi.started;
		this.showRecycle = showRecycle;
		this.iconManager = iconManager;
		tipo = 3;
		try {
			ci = parent.getPackageManager().getServiceInfo(rsi.service, 0);
		} catch (NameNotFoundException e) {
		}
		sb = new StringBuffer(packageName);
		sb.append(Setup.DELIMITER);
		sb.append(className);

	}

	public String getPackageName() {
		return packageName;
	}
	public String getClassName() {
		return className;
	}
	public View getView(int margin, BitmapDrawable bmpCestino, boolean dontShowTitle, int iconSize) throws NameNotFoundException {
		return getView(margin, bmpCestino, false, dontShowTitle, iconSize);
	}

	public void setColorTextShape(int valore) {
		resIdBackgroundText = valore;
	}
	public View getView(int margin, boolean dontShowTitle, int iconSize) throws NameNotFoundException {
		return getView(margin, null, false, dontShowTitle, iconSize);
	}
	public View getView(BitmapDrawable bmpCestino) throws NameNotFoundException {
		return getView(0, bmpCestino, false, false, 0);
	}
	public View getView(int margin, BitmapDrawable bmpCestino, boolean isService, boolean dontShowTitle, int iconSize) throws NameNotFoundException {
		return getView(0, bmpCestino, isService, dontShowTitle, iconSize, false, null);
	}
	public View getView(int margin, BitmapDrawable bmpCestino, boolean isService, boolean dontShowTitle, int iconSize, boolean reflection) {
		return getView(margin, bmpCestino, isService, dontShowTitle, iconSize, reflection, null);
	}
	public View getView(int margin, BitmapDrawable bmpCestino, boolean isService, boolean dontShowTitle, int iconSize, boolean reflection, XmlPullParser xml) {
		ImageView iv = null;
		TextView tv = null;
		Drawable img = null;
		View vApp = null;
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		lp.setMargins(margin, 0, margin, 0);

		LinearLayout ll = null;

		if (xml == null) {
			if (oclKillActivity == null) {
				vApp = parent.getLayoutInflater().inflate(R.layout.app_recently, null);
			} else {
				vApp = parent.getLayoutInflater().inflate(R.layout.app_running, null);
			}
		} else {
			vApp = View.inflate(parent, R.layout.app_running, null);
		}
		ll = (LinearLayout) ((LinearLayout) vApp).getChildAt(0);
		ll.setLayoutParams(lp);

		float widthImageIcon;
		float heightImageIcon;

		if (dontShowTitle) {
			ll.removeView(vApp.findViewById(R.id.TV_label));
			widthImageIcon = iconSize;
			heightImageIcon = iconSize;
		} else {

			String valore = "";
			if (ci.loadLabel(parent.getPackageManager()) == null) {
				valore = "";
			} else if (ci.loadLabel(parent.getPackageManager()) instanceof SpannedString) {
				valore = ((SpannedString) ci.loadLabel(pm)).toString();
			} else {
				valore = (String) ci.loadLabel(pm);
			}

			if ((valore == null) || ("".equals(valore))) {
				valore = (String) pm.getApplicationLabel(ci.applicationInfo);
			}
			tv = (TextView) vApp.findViewById(R.id.TV_label);
			tv.setText(valore);
			tv.setVisibility(View.VISIBLE);
			tv.setBackgroundResource(resIdBackgroundText);
			widthImageIcon = res.getDimension(android.R.dimen.app_icon_size);
			heightImageIcon = res.getDimension(android.R.dimen.app_icon_size);
		}

		if (reflection) {
			img = iconManager.ridimensionaAndReflection(ci.packageName,isGarbaged);
			if (img == null) {
				img = iconManager.ridimensionaAndReflection(ci.packageName, ci.loadIcon(pm), widthImageIcon, heightImageIcon, isGarbaged, showRecycle);
			}
		} else {
			img = iconManager.ridimensiona(ci.loadIcon(pm), widthImageIcon, heightImageIcon, isGarbaged, showRecycle);
		}

		iv = (ImageView) vApp.findViewById(R.id.IV_icon);
		iv.setOnClickListener(oclStartActivity);
		iv.setOnLongClickListener(oclKillActivity);
		iv.setTag(this);
		iv.setOnClickListener(oclStartActivity);

		iv.setImageDrawable(img);

		vApp.setMinimumWidth(60);

		img = null;
		return vApp;
	}
	@Override
	public String toString() {
		return sb.toString();
	}

	public boolean isRunning() {
		return tipo == 1;
	}
	public boolean isRecently() {
		return tipo == 2 || tipo == 4;
	}
	public boolean isPreferred() {
		return tipo == 2;
	}
	public boolean isService() {
		return tipo == 3;
	}

}
